home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / oclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.7 KB  |  286 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * oclip.c : openGL (motif) example showing how to use arbitrary clipping plane.
  20.  *
  21.  * Author : Yusuf Attarwala
  22.  *          SGI - Applications
  23.  * Date   : Mar 93
  24.  *
  25.  *    note : the main intent of this program is to demo the arbitrary
  26.  *           clipping functionality, hence the rendering is kept
  27.  *           simple (wireframe) and only one clipping plane is used.
  28.  *
  29.  *    press  left   button to move object 
  30.  *           right  button to move clipping plane
  31.  *
  32.  *
  33.  *---------------------------------------------------------------------------*/
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36.  
  37. #include <Xm/Xm.h> 
  38. #include <Xm/Frame.h>               /* for frame widgets */
  39. #include <Xm/Form.h>               /* for frame widgets */
  40. #include <X11/keysym.h>             /* keyboard translations */
  41. #include <X11/StringDefs.h>
  42.  
  43. #include <GL/gl.h>                  /* gl includes */
  44. #include <GL/glu.h>                 /* utility library includes */
  45. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  46.  
  47. /* function declarations */
  48.  
  49. void 
  50.     createToplevel(void),
  51.     drawScene(void),
  52.     setMatrix(void),
  53.     animateClipPlane(void),
  54.     animation(void),
  55.     exposeCB(Widget,XtPointer,XtPointer),
  56.     resizeCB(Widget,XtPointer,XtPointer),
  57.     initCB(Widget,XtPointer,XtPointer),
  58.     inputCB(Widget,XtPointer,XtPointer);
  59.  
  60.  
  61. /* global variables */
  62.             
  63. Display       *display;             /* current display */
  64. XtAppContext  appContext;           /* X application context */
  65. float         ax,ay,az;             /* angles for animation */
  66. GLUquadricObj *quadObj;             /* used in drawscene */
  67. GLdouble planeEqn[] = {0.707,0.707,0.0,0.0};   /* initial clipping plane eqn */
  68.  
  69. Widget        toplevel,       /* toplevel shell */
  70.               glw;            /* current widget */
  71.              
  72. GLXContext    glxc;           /* current glx context */
  73.  
  74. Arg args[20];
  75. int acnt;
  76.  
  77. void 
  78. main(int argc, char** argv)
  79. {
  80.     XtToolkitInitialize();
  81.     appContext = XtCreateApplicationContext();
  82.     display    = XtOpenDisplay(appContext, NULL, "Oclip","oclip",NULL,0,
  83.                               &argc,argv);
  84.     if (!display) {
  85.         printf("%s : Unable to open display\n",argv[0]);
  86.         exit(0);
  87.     }
  88.  
  89.     printf("\n---------------------------------------------\n");
  90.     printf("OpenGL arbitrary clipping plane example \n\n");
  91.     printf("Press:  left   button to move object\n\
  92.         right  button to move clipping plane\n");
  93.  
  94.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  95.     createToplevel();             /* create widget hierarchy */
  96.     XtAppMainLoop(appContext);    /* loop forever */
  97. }
  98.  
  99.  
  100. void
  101. createToplevel(void)
  102. {
  103.     Widget frame;
  104.  
  105.     acnt = 0;
  106.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  107.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  108.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  109.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  110.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  111.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  112.     toplevel  = XtAppCreateShell("openGL clip","xtext",
  113.                                   applicationShellWidgetClass,
  114.                                   display,args,acnt);
  115.  
  116.     /* create a frame to hold glx widget */
  117.     frame   = XtVaCreateManagedWidget("frame", 
  118.                   xmFrameWidgetClass, toplevel, 
  119.                   NULL);
  120.  
  121.     /* create a double buffer widget, in rgb mode and manage it */
  122.     acnt = 0;
  123.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  124.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  125.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  126.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  127.     XtManageChild(glw);
  128.  
  129.     /* register callbacks */
  130.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  131.  
  132.     /* realize widget hierarchy */
  133.     XtRealizeWidget(toplevel);
  134.  
  135.     /* additional callbacks */
  136.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  137.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  138.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  139.  
  140. }
  141.  
  142. void
  143. drawScene(void)
  144. {
  145.     glClearColor(0.0, 0.0, 0.0, 0.0);
  146.     glClear(GL_COLOR_BUFFER_BIT);
  147.  
  148.     glPushMatrix();
  149.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  150.     glColor3f (1.0, 1.0, 0.0);
  151.     glRotatef (ax,1.0,0.0,0.0);
  152.     glRotatef (-ay,0.0, 1.0, 0.0);
  153.  
  154.     glClipPlane(GL_CLIP_PLANE0,planeEqn);      /* define clipping plane */
  155.     glEnable(GL_CLIP_PLANE0);                  /* and enable it */
  156.  
  157.     gluCylinder (quadObj, 2.0,5.0,10.0,20,8);  /* draw a cone */
  158.  
  159.     glDisable(GL_CLIP_PLANE0);
  160.     glPopMatrix();
  161.  
  162.     glFlush();
  163.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  164.  
  165. }
  166.  
  167. void
  168. setMatrix(void)
  169. {
  170.     glMatrixMode(GL_PROJECTION);
  171.     glLoadIdentity();
  172.     glOrtho(-15.0,15.0,-15.0,15.0,-10.0,10.0);
  173.     glMatrixMode(GL_MODELVIEW);
  174.     glLoadIdentity();
  175. }
  176.  
  177. void
  178. animation(void)
  179. {
  180.     register int i;
  181.     /* animate the cone */
  182.  
  183.     for (i=0;i<60;i++) {
  184.         ax += 5.0;
  185.         ay -= 2.0;
  186.         az += 5.0;
  187.         if (ax >= 360)  ax = 0.0;
  188.         if (ay <= -360) ay = 0.0;
  189.         if (az >= 360)  az = 0.0;
  190.         drawScene();
  191.     }
  192. }
  193.  
  194. void
  195. animateClipPlane(void)
  196. {
  197.     register int i;
  198.     static int sign = 1;
  199.  
  200.     for (i=0;i<5;i++) {
  201.     planeEqn[3] += sign*0.5;
  202.     if (planeEqn[3] > 4.0) sign = -1;
  203.     else if (planeEqn[3] < -4.0) sign = 1;
  204.         drawScene();
  205.     }
  206. }
  207.  
  208. void 
  209. inputCB(Widget w, XtPointer client_data, XtPointer call)
  210. {
  211.     char            string[31];
  212.     XComposeStatus  composeStatus;
  213.     KeySym keysym;
  214.     GLwDrawingAreaCallbackStruct *call_data;
  215.  
  216.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  217.  
  218.     switch(call_data->event->type) {
  219.     case ButtonPress:
  220.         switch (call_data->event->xbutton.button) {
  221.         case Button1:
  222.         animation();
  223.             break;
  224.         case Button2 :
  225.         printf("I (button2) don't do anything\n");
  226.             break;
  227.         case Button3 :
  228.         animateClipPlane();
  229.             break;
  230.         }
  231.         break;
  232.     case KeyPress :
  233.         XLookupString(&call_data->event->xkey,string,
  234.                       30,&keysym,&composeStatus);
  235.         switch(keysym) {
  236.         case XK_Escape :
  237.             exit(0);
  238.             break;
  239.         }
  240.     break;
  241.     default:
  242.         break;
  243.     }
  244. }
  245.  
  246. void 
  247. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  248. {
  249.     GLwDrawingAreaCallbackStruct *call_data;
  250.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  251.  
  252.  
  253.     GLwDrawingAreaMakeCurrent(w, glxc);
  254.     glViewport(0, 0, call_data->width, call_data->height);
  255.     setMatrix();
  256.     drawScene();
  257. }
  258.  
  259. void 
  260. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  261. {
  262.     GLwDrawingAreaCallbackStruct *call_data;
  263.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  264.  
  265.  
  266.     GLwDrawingAreaMakeCurrent(w, glxc);
  267.     glViewport(0, 0, call_data->width, call_data->height);
  268.     drawScene();
  269. }
  270.  
  271. void
  272. initCB(Widget w, XtPointer client_data, XtPointer call)
  273. {
  274.     XVisualInfo *vi;
  275.  
  276.  
  277.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  278.     XtGetValues(w, args, 1);
  279.  
  280.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  281.  
  282.     ax = 10.0;
  283.     ay = -10.0;
  284.     az = 0.0;
  285. }
  286.